import { ECSEntity } from "./ECSEntity";
import { Constructor } from "cc";
import { ECSBase } from "./ECSBase";

export class ECSCmp extends ECSBase {
    private _entity: ECSEntity | null = null;
    get entity(): ECSEntity | null {
        return this._entity;
    }

    constructor(entity: ECSEntity) {
        super();
        this._entity = entity;
    }

    /** 获取同实体组件 */
    getComponent<T extends ECSCmp>(type: Constructor<T>): T | null {
        return this._entity?.getComponent(type);
    }

    /** 检查同实体组件 */
    hasComponent<T extends ECSCmp>(type: Constructor<T>): boolean {
        return this._entity?.hasComponent(type);
    }

    /**************************内部使用*********************** */
    _ecsEnable(): void {
        this.onEnable()
    }

    _ecsDisable(): void {
        this.onDisable()
    }

    _ecsUpdate(dt: number): void {
        if (this.enable) this.onUpdate(dt);
    }

    _ecsDestroy(): void {
        this._entity = null;
        this.onDestroy();
    }
    /**************************内部使用*********************** */
}